package cn.yqx.bankshop.order.service.impl;

import cn.yqx.bankshop.order.dao.BuMyTrackMapper;
import cn.yqx.bankshop.order.page.PageRequest;
import cn.yqx.bankshop.order.page.PageResult;
import cn.yqx.bankshop.order.page.PageUtils;
import cn.yqx.bankshop.order.pojo.BuMyTrack;
import cn.yqx.bankshop.order.service.GoodsService;
import cn.yqx.bankshop.order.service.TrackService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TrackServiceImpl  implements TrackService {
    @Autowired
    BuMyTrackMapper mapper ;
    @Autowired
    GoodsService goodsService ;

    @Override
    public PageResult getAllTrack(PageRequest request, Map<String, Object> map) {
       return PageUtils.getPageResult(request, getPageInfoTrack(request,map)) ;
    }

    private PageInfo<BuMyTrack> getPageInfoTrack(PageRequest pageRequest, Map<String ,Object> parameterMap){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<BuMyTrack> list = mapper.getAllTrack(parameterMap) ;
        //处理数据
        List<BuMyTrack> listNew = new ArrayList<BuMyTrack>() ;
        if(!list.isEmpty()){
            list.stream().forEach(item->{

                if(StringUtils.isEmpty(item.getGoodsName())){
                    Map<String,Object> map =   goodsService.findById(item.getGoodsId().longValue()) ;
                    log.info("map : {} " ,  map );
                    if(map !=null){
                        item.setGoodPicture((String) map.get("goodsPhoto"));
                        item.setGoodsName((String)map.get("goodsName"));
                        item.setGoodsUnitPrice((Double)map.get("goodsUnitPrice"));
                        listNew.add(item) ;
                    }

                }else{
                    listNew.add(item) ;
                }
            });
        }
        log.info("NEW list IS {} ",JSON.toJSONString(list));
        return new PageInfo<BuMyTrack>(listNew) ;
    }
}
